package com.drought.rained;

import android.content.Context;
import android.support.multidex.MultiDexApplication;
import android.text.TextUtils;

import com.alibaba.android.arouter.launcher.ARouter;
import com.appsflyer.AppsFlyerConversionListener;
import com.appsflyer.AppsFlyerLib;
import com.drought.rained.cache.AcacheLJ;
import com.drought.rained.cache.AcacheKeysLJ;
import com.drought.rained.common.ConstantLJ;
import com.drought.rained.business.ParamsFactoryLJ;
import com.scwang.smartrefresh.layout.footer.ClassicsFooter;
import com.scwang.smartrefresh.layout.header.ClassicsHeader;

import java.util.Map;

public class LJApplication extends MultiDexApplication {
    static {
        ClassicsHeader.REFRESH_HEADER_PULLDOWN = "Tarik kebawah untuk refresh";
        ClassicsHeader.REFRESH_HEADER_REFRESHING = "sedang refresh...";
        ClassicsHeader.REFRESH_HEADER_LOADING = "sedang memuat...";
        ClassicsHeader.REFRESH_HEADER_RELEASE = "lepaskan untuk refresh";
        ClassicsHeader.REFRESH_HEADER_FINISH = "selesai memuat";
        ClassicsHeader.REFRESH_HEADER_FAILED = "gagal memuat";
        ClassicsHeader.REFRESH_HEADER_LASTTIME = "'Pembaharuan Terakhir' M-d HH:mm";
        ClassicsFooter.REFRESH_FOOTER_PULLUP = "ttarik keatas untuk memuat lebih banyak";
        ClassicsFooter.REFRESH_FOOTER_RELEASE = "lepaskan untuk memuat";
        ClassicsFooter.REFRESH_FOOTER_LOADING = "sedang memuat...";
        ClassicsFooter.REFRESH_FOOTER_FINISH = "selesai memuat";
        ClassicsFooter.REFRESH_FOOTER_FAILED = "gagal memuat";
        ClassicsFooter.REFRESH_FOOTER_ALLLOADED = "berhasil memuat semua";
    }
    private static Context mContext;

    public static Context getContext() {
        return mContext;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        mContext = this;
        initArouter();
        initAppFlyer();
    }

    private void initArouter() {
        if (BuildConfig.DEBUG) {
            ARouter.openLog();     // 打印日志
            ARouter.openDebug();   // 开启调试模式(如果在InstantRun模式下运行，必须开启调试模式！线上版本需要关闭,否则有安全风险)
        }
        ARouter.init(this); // 尽可能早，推荐在Application中初始化
    }

    private void initAppFlyer() {
        AppsFlyerConversionListener conversionDataListener = new AppsFlyerConversionListener() {
                    @Override
                    public void onInstallConversionDataLoaded(Map<String, String> map) {
                        if (map.containsKey(AcacheKeysLJ.MEDIA_SOURCE)) {
                            String mediaSource = map.get(AcacheKeysLJ.MEDIA_SOURCE);
                            if (!TextUtils.isEmpty(mediaSource)) {
                                AcacheLJ.get(getContext()).put(AcacheKeysLJ.MEDIA_SOURCE,
                                        mediaSource);
                                ParamsFactoryLJ.unChangeCommonParams = null;
                                ParamsFactoryLJ.installCommonParams = null;
                            }
                        }
                    }

                    @Override
                    public void onInstallConversionFailure(String s) {}

                    @Override
                    public void onAppOpenAttribution(Map<String, String> map) {}

                    @Override
                    public void onAttributionFailure(String s) {}
                };
        AppsFlyerLib.getInstance().init(ConstantLJ.AF_DEV_KEY, conversionDataListener, getApplicationContext());
        AppsFlyerLib.getInstance().startTracking(this);
    }
}
